﻿using ERPSystem.Common.Domains.supplier;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using Microsoft.EntityFrameworkCore.Query.Internal;
using SqlSugar;

namespace BasicInformationCommon.API.BasicInformationCommons.Suppliers
{
    public class PurchaseOrderFindHandler : IRequestHandler<PurchaseOrderFindCommand, Result<PurchaseOrder>>
    {
        private readonly IBaseRepository<PurchaseOrder> purchaseOrderRepository;

        public PurchaseOrderFindHandler(IBaseRepository<PurchaseOrder> purchaseOrderRepository)
        {
           this.purchaseOrderRepository = purchaseOrderRepository;
        }

        public async Task<Result<PurchaseOrder>> Handle(PurchaseOrderFindCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PurchaseOrder>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var purchaseOrder = await (await purchaseOrderRepository.GetAll()).Where(x => x.PurchaseOrderId == request.PurchaseOrderId).FirstAsync();


                result.Data = purchaseOrder;

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
